#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>

int main()
{
    //char* env_val = "MYENV=11111111111";
    //putenv(env_val);
    //extern char** environ;
    
    char* const myenv[] = // 自定义环境变量表
    {
        "MYENV1 = 11111111111111111",
        "MYENV2 = 22222222222222222",
        "MYENV3 = 33333333333333333",
        "MYENV4 = 44444444444444444",
        NULL 
    };

    pid_t id = fork();
    if(0 == id)
    {
        // child
        printf("pid: %d, exec command begin\n", getpid());
        execle("./mytest", "mytest", "-a", "-l", NULL, myenv);
        
        //execl("./mytest", "mytest", NULL);
        //char* const argv[] = {"ls", "-a", "-l", NULL};
        //printf("pid: %d, exec command begin\n", getpid());
        //sleep(1);
        //execvp("ls", argv);
        //execv("/usr/bin/ls", argv);
        //execlp("ls", "ls", "-a", "-l", NULL);
        //execl("/usr/bin/ls", "ls", "-l", "-a", NULL); // 因为可变参数列表，所以以NULL结尾，表示参数传完了
        printf("pid: %d, exec command end\n", getpid());
    }
    else
    {
        // father
        pid_t rid = waitpid(-1, NULL, 0);
        if(rid > 0)
        {
            printf("wait success, rid: %d\n", rid);
        }
    }
    return 0;
}
